<script setup>
import router from "../../../router/index.js";
import MyNav from "../../../components/MyNav.vue";
import MyHead from "../../../components/MyHead.vue";
import MyTable from "../../../components/MyTable.vue";
import MyPager from "../../../components/MyPager.vue";
import {onMounted, reactive, ref} from "vue";
import {deleteApi, deleteBatchApi, pageApi} from "../../../util/axios.js";
import {myPage} from "../../../util/request.js";
import {isNotEmpty, isNotNull} from "../../../util/index.js";
import {notYet} from "../../../util/index.js";

/* ==================== 页面常量 ==================== */

// 路径导航
const navItems = [
  {icon: 'Notebook', label: '课程管理'},
  {icon: 'Comment', label: '评论列表'},
];

// 页面头
const headItems = [
  {type: 'ipt', span: 5, placeholder: '按视频集ID搜索', callback: pageByEpisodeId},
  {type: 'ipt', span: 5, placeholder: '按评论人ID搜索', callback: pageByUserId},
];

// 表格列
const columns = [
  {label: '被评论视频', prop: 'episode.title'},
  {label: '评论人昵称', prop: 'nickname'},
  {label: '评论内容', prop: 'content', type: 'card', width: 500, tooltip: false},
];

// 表格按钮
const buttons = [
  {label: '回复列表', type: 'success', callback: commentReply}
];

/* ==================== 分页查询 ==================== */

// 表格行 + 分页 + 视频集ID + 评论人ID
let records = ref();
let pageInfo = reactive({pageNum: 1, pageSize: 5, callback: page});
let episodeId = ref();
let userId = ref();

/**
 * 分页查询
 *
 * 1. 定义分页基础配置，包括 records, pageInfo, api, params 等。
 * 2. 附加分页查询条件，如视频集ID，评论人ID等。
 * 3. 异步发送分页查询请求。
 *
 * @param pageNum 当前第几页，默认 1
 * @param pageSize 每页多少条，默认 5
 */
async function page(pageNum = pageInfo['pageNum'],
                    pageSize = pageInfo['pageSize']) {
  let config = {
    records, pageInfo,
    api: pageApi,
    params: {pageNum, pageSize},
    args: {module: 'comment'},
  }
  if (isNotEmpty(episodeId.value)) config['params']['fkEpisodeId'] = episodeId.value;
  if (isNotEmpty(userId.value)) config['params']['fkUserId'] = userId.value;
  await myPage(config);
}

/* ==================== 按视频集ID查询 ==================== */

/**
 * 按视频集ID查询
 *
 * 1. 将输入框中的值赋值给分页条件字段变量。
 * 2. 重新发送分页请求。
 *
 * @param val 输入框中的值
 */
function pageByEpisodeId(val) {
  if (isNotNull(val) || episodeId.value) {
    episodeId.value = val;
    page();
  }
}

/* ==================== 按评论人ID查询 ==================== */

/**
 * 按评论人ID查询
 *
 * 1. 将输入框中的值赋值给分页条件字段变量。
 * 2. 重新发送分页请求。
 *
 * @param val 输入框中的值
 */
function pageByUserId(val) {
  if (isNotNull(val) || userId.value) {
    userId.value = val;
    page();
  }
}

/* ==================== 回复列表 ==================== */

/**
 * 查询回复列表
 *
 * 1. 存储评论主键 pid
 * 2. 路由到 Reply 页面
 *
 * @param row 表格行
 */
function commentReply(row) {
  sessionStorage.setItem('pid', row['id']);
  router.push('/Reply')
}

/* ==================== 加载函数 ==================== */

onMounted(() => page());

</script>

<template v-if="records">
  <my-nav :items="navItems"/>
  <my-head :items="headItems"/>
  <my-table class="comment-list-table"
            module="comment"
            insert-page="/CommentInsert"
            update-page="/CommentUpdate"
            :delete-api="deleteApi"
            :delete-batch-api="deleteBatchApi"
            :delete-callback="page"
            :excel-api="notYet"
            :records="records"
            :columns="columns"
            :buttons="buttons"/>
  <my-pager :page-info="pageInfo"/>
</template>

<style scoped lang="scss">

</style>